﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HighDivePrinter.Model;

using System.Xml;
using System.Xml.Serialization;
using System.ComponentModel;

namespace HighDivePrinter.Model
{
    /// <summary>
    ///Class for contest management
    /// </summary>
    [Serializable()]
    public class Contest : IComparable<Contest>
    {
        #region Properties

        /// <summary>
        /// DatabaseId
        /// </summary>
        /// 
        private int? _DbId;
        private string _Name;
        private string _Date;
        private string _Club;
        private string _City;
        private string _Country;
        private ContestStatus _Status;
        //private JudgeList _JudgeList;
        private DiveList _DiveQueue;
        private DiveList _DiveHistory;

        /*public int? DbId
        {
             get
            {
                return _DbId;
            }
            set
            {
                _DbId = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DbId"));
            }
        }*/
        public string Name 
        {
             get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }
       
        public string Club 
        {
            get
            {
                return _Club;
            }
            set
            {
                _Club = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Club"));
            }
        }
        public string City
        {
             get
            {
                return _City;
            }
            set
            {
                _City = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("City"));
            }
        }
        public string Country 
        {
            get
            {
                return _Country;
            }
            set
            {
                _Country = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Country"));
            }
        }

        /// <summary>
        /// ContestStatus determines whether the competition is Unstarted / Started / Finished
        /// </summary>
        public ContestStatus Status 
        {
            get
            {
                return _Status;
            }
            set
            {
                _Status = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Status"));
            }
        }
        public string Date
        {
            get
            {
                return _Date;
            }
            set
            {
                _Date = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Date"));
            }
        }
        /// <summary>
        /// List of the contest judges
        /// </summary>
     /*   public JudgeList JudgeList 
        {
             get
            {
                return _JudgeList;
            }
            set
            {
                _JudgeList = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("JudgeList"));
            }
        }*/

        /// <summary>
        /// Queue of future jumpers. This list is empty when the contest ended.
        /// </summary>
      /*  public DiveList DiveQueue 
        {   
             get
            {
                return _DiveQueue;
            }
            set
            {
                _DiveQueue = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DiveQueue"));
            }
        }

        /// <summary>
        /// History of the performed jumps. This list is full when the contest ended.
        /// </summary>
        public DiveList DiveHistory 
        {
             get
            {
                return _DiveHistory;
            }
            set
            {
                _DiveHistory = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DiveHistory"));
            }
        }
        */
        #endregion

        #region Public methods

        /// <summary>
        /// Add a new convictions or points 
        /// update an already set points
        /// Check if JudgeScore list is complete, if so then               
        /// call TotalScore().
        /// </summary>
     /*   public void UpdateDiveScore(DiveScore score, Dive dive)
        {            
          /*  if (Log.IsDebugEnabled)
            {
                Log.Debug("Dive " + dive.QueueOrderNumber + " score updated by " + score.Judge.IdString + " to " + score.Score);
            }*/
/*
            if (!dive.DiveScoreList.Exists(x => x.Judge.IdString == score.Judge.IdString))
            {
                dive.DiveScoreList.Add(score);
            }
            else
            {
                dive.DiveScoreList.Find(x => x.Judge.IdString == score.Judge.IdString).Score = score.Score;
            }

           // Log.Info("Dive " + dive.QueueOrderNumber + " score updated by " + score.Judge.IdString + " to " + score.Score);            
        }*/

        /// <summary>
        /// Moves the first dive in the dive queue into the dive history.
        /// </summary>
        public void UpdateDiveQueue()
        {
            /*if (Log.IsDebugEnabled)
            {
                Log.Debug("Dive " + DiveQueue[0].QueueOrderNumber + " (DbId:" + DiveQueue[0].DbId + ") moved from queue to history"); 
            }
            DiveQueue[0].Date = DateTime.Now.Year.ToString("0000") + "-" + DateTime.Now.Month.ToString("00") + "-" + DateTime.Now.Day.ToString("00") + "-" +
                                  DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00");
            DiveHistory.Insert(0, (DiveQueue[0]));
            DiveQueue.RemoveAt(0);
            */
        }

        /// <summary>
        /// Checks that all judge scores have been entered for a dive
        /// </summary>
        /// <param name="dive">The dive which to check</param>
        /// <returns>true if all scores have been entered, otherwise false</returns>
     /*   public bool CheckScoreComplete(Dive dive)
        {
            return dive.DiveScoreList.Count == JudgeList.NumberOfJudges;
        }
        */
        public override string ToString()
        {
            return Name + ": " + Date + " | " + Club;
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new contest.
        /// </summary>
        public Contest()
        {
            Status = ContestStatus.Unstarted;
            //DbId = null;
            Name = null;
            Date = null;
            Club = null;
            City = null;
            Country = null;
            //DiveHistory = null;
            //DiveQueue = null;
           // JudgeList = null;
        }

        #endregion

        #region IComparable Members

        /// <summary>
        /// Method that defines comparison of two Contest.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(Contest obj)
        {
            int c = this._Date.CompareTo(obj._Date);
            if (c == 0)
                return this._DbId.Value.CompareTo(obj._DbId.Value);
            else
                return c;
        }

        #endregion

        #region INotifyPropertyChanged Members
        /// <summary>
        /// Event declaration.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
